/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tyae.filtro;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class FiltroLogin implements Filter {

    private HttpSession session;

    public FiltroLogin() {
    } 


    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
	throws IOException, ServletException {

    
	    HttpServletRequest rq = (HttpServletRequest)request;
	    HttpServletResponse rp = (HttpServletResponse)response;
	    
	    
	    session = rq.getSession();
	    boolean auth = session.getAttribute("usr") != null;
	    
	    
	    if ( ! rq.getRequestURL().toString().contains("login.jsf") && 
	     	 ! rq.getRequestURL().toString().contains("principal.jsf") &&
	     	 ! rq.getRequestURL().toString().contains("js.jsf") &&
	     	 ! rq.getRequestURL().toString().contains("css.jsf") &&
	     	 ! rq.getRequestURL().toString().contains(".png") &&
	     	 ! rq.getRequestURL().toString().contains(".jpg") &&
	     	 ! rq.getRequestURL().toString().contains(".bmp") &&
	    	 ! rq.getRequestURL().toString().contains("jsf.js.jsf") ){
		    if (! auth ) {
		    	session.setAttribute("url", rq.getRequestURL().toString());
		    	rp.sendRedirect("/sysjuridico/login.jsf");
		        return;
		    }
	   
	    }
	    
		try {
		    chain.doFilter(request, response);
		}
		catch(Throwable t) {
		    t.printStackTrace();
		}

    }


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
    
   
}
